<?php
namespace app\api\controller\v1;

use app\admin\model\Area;
use app\common\controller\Api;
use app\common\model\Area as AreamModel;
use app\common\model\Banking;
use app\common\model\LitestoreShop as LitestoreShopModel;
use app\common\service\LitestoreShopCategory;

class Config extends Api {
    protected $noNeedLogin = ['commonInfo','getAreaInfo','clearArea','getProvince','getcategory','getBank'];
    protected $noNeedRight = '*';
    /**
     * 获取系统配置
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
        public function commonInfo(){
            //地区配置 换成文件缓存
            $AreamModel = new AreamModel();
            $Config = cache('DB_CONFIG_DATA');
//            $map=['pid'=>0];

            if (!$Config) {
                //获取所有系统配置
                $Banking = new Banking();
                $Config['banking'] =  $Banking->select();
                $Config['proxy']=[
                    'provincial_level'=>10,
                    'city_level'=>8,
                    'region_level'=>5,
                    'personal_level'=>2,
                ];
                $Config['invit_url']='index/index/index/code/';
                $map['pid']=0;
                $Config['area']=$AreamModel->where($map)->field('id,name,level,price')->select();
                //地图
                $city  = $AreamModel->field('id,name,level,pid,price')->select();
                $city_arr =  collection($city)->toArray();
                $Config['city'] = city_merge($city_arr,0);
//                $Config['litestore_category'] = ;
//                $Config['bank'] = ;
                $LitestoreShopCategory =  new LitestoreShopCategory();
                $Config['store_shop_category'] =  $LitestoreShopCategory->select([],'id,name');

                cache('DB_CONFIG_DATA', null);
                cache('DB_CONFIG_DATA', $Config, 36000); //缓存配置
            }
            $this->success("请求成功",$Config);
//            return resultArray(['data' => $Config]);
        }

    /**
     * 获取城市下信息
     */
    public function getAreaInfo(){
        $id =   $this->request->request('id');
        if($id){
            $area = \app\common\service\Area::getAreaChild($id);
            $this->success("请求成功",$area);
        }
        return json(array('code'=>0,"data"=>[],'msg'=>"暂无数据"));
    }
    public function clearArea(){
        cache('DB_CONFIG_DATA', null);
        echo 'success';
    }

    /**
     * 获取所有省份
     */
    public function getProvince(){
        try{
            $area=new Area();
            $data=$area->where([' pid'=>0])->select();
            if($data){
                $data=collection($data)->toArray();
            }else{
                $data=[];
            }
        }catch (\Exception $e){
            $this->error('请求失败',[]);
        }
        $this->success('请求成功',$data);
    }

    /**
     * 获取所有行业
     */
    public function getcategory(){
        try{
            $Category=new \app\common\model\LitestoreShopcategory();
            $data=$Category->select();

            if(empty($data)){
                $this->error('请求无数据',[]);
            }
            $data=collection($data)->toArray();
        }catch (\Exception $e){
            $this->error('请求失败',[]);
        }
        $this->success('请求成功',$data);

    }

    public function getBank(){
        try{
            $Banking = new Banking();
            $data =  $Banking->select();
            if(empty($data)){
                $this->error('请求无数据',[]);
            }
            $data=collection($data)->toArray();
        }catch (\Exception $e){
            $this->error('请求失败',[]);
        }
        $this->success('请求成功',$data);
    }

    public function temp(){
        try{
            $site=\think\Config::get('site');
            if(!empty($site['cash_min'])){
                $data['cash_min']=$site['cash_min'];
            }else{
                $data['cash_min']=0;
            }

            if(!empty($site['personal_level'])){
                $data['personal_level']=$site['personal_level'];
            }else{
                $data['personal_level']=0;
            }

            if(!empty($site['cash_beishu'])){
                $data['cash_beishu']=$site['cash_beishu'];
            }else{
                $data['cash_beishu']=0;
            }
        }catch (\Exception $e){
            $this->error('请求失败',[]);
        }

        $this->success('请求成功',$data);
    }







}